package com.book.lizhi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import cn.waps.AppConnect;

import com.book.lizhi.BaseActivity;
import com.book.lizhi.R;
import com.book.lizhi.ui.fragment.ChapterFragment;
import com.book.lizhi.ui.fragment.DetailFragment;
import com.book.lizhi.widget.SlidingTabLayout;

public class DetailActivity extends BaseActivity {

	private SlidingTabLayout mIndicator;
	private ViewPager mViewPager;

	private Fragment[] mContentFragments;
	private String[] TITLES = { "封面", "目录" };


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toobar);
		setSupportActionBar(toolbar);
		initUI();

	}

	private void initUI() {

		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setTitle("书籍详情");
		
		mContentFragments = new Fragment[2];
		mContentFragments[0] = new DetailFragment();
		mContentFragments[1] = new ChapterFragment();

		PagerAdapter mAdapter = new DetailAdapter(getSupportFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mAdapter);
		mIndicator = (SlidingTabLayout) findViewById(R.id.indicator);
		mIndicator.setViewPager(mViewPager);
		mIndicator.setSelectedIndicatorColors(getResources().getColor(R.color.green_14be7d));
		
		LinearLayout linearLayout=(LinearLayout) findViewById(R.id.adv_container);
		AppConnect.getInstance(this).showBannerAd(this, linearLayout);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.detail, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		} else if (id == R.id.action_bookshelf) {
			startActivity(new Intent(getApplicationContext(), NavActivity.class));
		}
		return super.onOptionsItemSelected(item);
	}



	class DetailAdapter extends FragmentPagerAdapter {

		public DetailAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return mContentFragments[arg0];
		}

		@Override
		public int getCount() {
			return mContentFragments.length;
		}
		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

	}
}
